<?php
namespace Admin\Model;
use Think\Model;

class MenuModel extends Model {

    public static $_menu;

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        self::$_menu = $this->getMenuList();
    }


    /**
     * 递归获取后台管理菜单
     * @param int $pid
     * @return array
     */
    protected function getMenuList($pid = 0) {

        $map['pid'] = $pid;
        $map['hidden'] = 0;
        $map['status'] = array('neq',0);
        $map['_string'] = 'role = "common" OR role = "' . session ('role') . '"';
        $res = $this->where($map)->order('sort asc')->select();

        if (!$res || count($res) == 0) return '';

        foreach ($res as $k=>$v) {
            $url = ($v['controller'] !== '#')? __APP__ . '/' . $v['controller']:$v['controller'];
            $result[] = array(
                'id' => $v['id'],
                'text' => $v['name'],
                'controller' => $v['controller'],
                'url' => $url,
                'active' => '',
                'icon' => $v['icon'],
                'child' => $this->getMenuList($v['id'])
            );
        }
        return $result;
    }
}